package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class shoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.判断当前加入购物车中的商品是否已经存在了
        //新建购物车 将前端传来的数据拷贝
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //通过拦截器获取用户ID
        final Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //调用mapper 创建购物车 此处只能查出一条数据   方便后面复用
        final List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //2.如果已经存在了  只需要将数量+1
        if (list !=null &&list.size()>0){
            final ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //如果不存在，插入数据，数量就是1

            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //添加到购物车的是菜品
                //设置菜品属性
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //添加到购物车的是套餐
                //设置套餐属性
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {

        //获取当前微信用户的Id
        final Long userId = BaseContext.getCurrentId();
        final ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        final List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }


    /**
     * 清空购物车商品
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //新建购物车 将前端传来的数据拷贝
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查询当前用户的购物车数据  设置查询条件
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询购物车的一条数据
         List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
          //判断当前购物车是否不为空
        if (list !=null &&list.size()>0){
             shoppingCart = list.get(0);
            final Integer number = shoppingCart.getNumber();
            //判断购物车商品数量是否为1
            if (number ==1){
                //为1  直接删除改商品
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else{
                //不为1  数量-1
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                //更新购物车
                shoppingCartMapper.updateNumberById(shoppingCart);
            }

        }
    }
}
